package com.pilgrim.generator.controller;

import com.pilgrim.generator.vo.LoginUserVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

/**
 * AbstractBaseController
 *
 * @author wyb
 * @version 1.0.0
 */
public abstract class AbstractBaseController {

    /**
     * 获取当前线程的登录用户user对象
     *
     * @return LoginUserEntity
     */
    public static final LoginUserVO getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (Objects.nonNull(authentication)) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof LoginUserVO) {
                return (LoginUserVO)principal;
            }
        }
        return null;
    }

    /**
     * 获取当前线程的登录用户user对象
     *
     * @return LoginUserEntity
     */
    public static final Long getLoginUserId() {
        LoginUserVO loginUser = getLoginUser();
        if (loginUser == null) {
            return null;
        }
        return loginUser.getUserId();
    }

}
